<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Mindex extends MY_Model {

    public function __construct() {
        parent::__construct();
    }

    public function checklikeDB() {
        $check = $this->m_db->where(array('mac' => $this->getParamString('mac'), 'nasid' => $this->getParamString('nasid')))->count('like_check');
        if ($check > 0) {
            return '200';
        }
    }

//    public function insertlikefbDB($mac, $nasid) {
//        //$check = $this->m_db->where(array('mac' => $mac, 'nasid' => $nasid))->count('like_check');
//        // die("CHECK:" . $check);
//        //if ($check == 0) {
//        //$this->m_db->insert('like_check', array('mac' => $mac, 'nasid' => $nasid));
//        $this->campaignlikefanpagefacebook($mac, $nasid);
//        return '200';
//        //}
//    }

    private function checkSession() {
        @$insert = intval(@$_SESSION['insert']);
        if ($insert == 0) {
            $_SESSION['insert'] = 1;
            return 1;
        } else {
            return 0;
        }
    }

    public function getIdPlace($called) {
        $id_place = $this->db->query('SELECT `id_place` FROM `modem` WHERE `nas_id` = "' . $called . '"')->row_object();
        if (count($id_place) > 0) {
            return $id_place->id_place;
        }
        return 0;
    }

    public function getCampaigns($id_place) {
        $wdate = date('Y-m-d H:i:s');
        $data1 = $this->db->query('SELECT cp.id_campaign AS id, c.link_redirect, c.type_ads, c.appcode, c.link_redirect,c.type_time_session, c.id_customers, c.oneview
			FROM `campaign_place` AS cp 
			INNER JOIN `campaign` AS c ON c.id = cp.id_campaign 
			WHERE c.`active` = 2 AND c.is_package = 1 AND cp.id_place = ' . $id_place . ' AND time_start  <= NOW() AND time_end >= NOW()'); //->c.is_package = 1 chiến dịch nội bộ tại địa điểm

        $data2 = $this->db->query('SELECT cp.id_campaign AS id, c.link_redirect, c.type_ads, c.appcode,c.link_redirect,c.type_time_session, c.id_customers, c.oneview 
			FROM `campaign_place` AS cp 
			INNER JOIN `campaign` AS c ON c.id = cp.id_campaign 
			WHERE c.`active` = 2 AND c.is_package != 1 AND cp.id_place = ' . $id_place . ' AND time_start  <= NOW() AND time_end >= NOW()'); //->c.is_package != 1; chiến dịch hệ thống tại địa điểm
			
		if(!in_array($data2->result_object(), array(null, ''))){
			$data = $data2->result_object();
		}else{
			$data = $data1->result_object();
		}
        //$data = array_merge($data1->result_object(), $data2->result_object());
		//rsort($data);
//echo "<pre>";die(print_r($data));
        $mac = $this->getParamString('mac') ? $this->getParamString('mac') : $this->session->userdata('mac');
        $sdata = array();
        foreach ($data AS $el => $le) {
            $type_ads = (int) $le->type_ads;

            if ($type_ads == 14) {

                $damount = $this->db->query('SELECT amount, amount_get, amount_used FROM campaign_coupon WHERE id_campaign = ' . $le->id)->row_object();
                if (!in_array($damount->amount, array('', null)) && !in_array($damount->amount_get, array('', null)) && (($damount->amount) > ($damount->amount_get))) {
                    $tbl_mongo = $this->db->query('SELECT tbl_mongo FROM ads_type WHERE id = ' . $type_ads)->row_object()->tbl_mongo;
                    if (in_array($tbl_mongo, array('', null))) {
                        $data[$el]->active = 0;
                    } else {
                        $check = $this->checkInteractType($mac, $le->id, $le->oneview, $tbl_mongo);
                        if ($check == '200') {
                            $sdata[] = $data[$el];
                        }
                    }
                }
            } else {
                $tbl_mongo = $this->db->query('SELECT tbl_mongo FROM ads_type WHERE id = ' . $type_ads)->row_object()->tbl_mongo;
                if (in_array($tbl_mongo, array('', null))) {
                    $data[$el]->active = 0;
                    $sdata[] = $data[$el];
                } else {
                    $check = $this->checkInteractType($mac, $le->id, $le->oneview, $tbl_mongo);
                    if ($check == '200') {
                        $sdata[] = $data[$el];
                    }
                }
            }
        }
        return $sdata;
    }

    public function getCampaignError($typeadv_id) {
        return $this->db->query("SELECT *  FROM `campaign` 
                WHERE `type_ads` = $typeadv_id LIMIT 1")->result_object();
    }

    public function getCampaignPreview($user_id) {
        return $this->db->query("SELECT `id`,`appcode`  FROM `user` 
                WHERE `id` = $user_id LIMIT 1")->result_object();
    }

    public function checkInteractType($mac, $id_campaign, $oneview, $tbl_mongodb) {
        $count_interact = $this->m_db->where(array('mac' => (string) $mac, 'id_campaign' => (int) $id_campaign))->count($tbl_mongodb);
        if ($count_interact >= $oneview) { // qúa số tương tác
            return '201';
        } else {
            return '200';
        }
    }

    public function getCampaign($id_campaign) {
        $data = $this->db->query('SELECT `id`, `appcode` FROM `campaign` WHERE `active` = 2 AND `id` = ' . $id_campaign);
        return $data->row_object();
    }

    public function insertLogAccess($mac, $nasid, $id_place, $id_campaign, $code) {
        ini_set('mongo.native_long', 1);
        require_once(PROJECT_PATH . '/getinfo/index.php');
        $info = new UAParser;
        $ua = $_SERVER['HTTP_USER_AGENT'];
        $result = $info->parse($ua);
        $BrowerName = $result->ua->family;
        @$exp_Os = explode(' ', $result->os->family);
        @$OsName = $exp_Os[0];
        $NameDevice = $result->device->family;
        $BrowerNameVer = $result->ua->toString;
        $OsNameVer = $result->os->toString;
        $NameDeviceVer = $result->device->family;
        @$visit = $this->getParamInt('visit') != 0 ? $this->getParamInt('visit') : 2;
        @$res = $this->getParamString('res') != null ? $this->getParamString('res') : $this->session->userdata('res');
        $Ip = @$this->get_client_ip();
        $checkSession = $this->checkSession();
        $nr = date('YmdHis');
        $time_vs = new MongoInt64($nr);
        $now = date("Y-m-d H:i:s", time());
        $_SESSION['insert'] = 1;
        $year = date('Y', time());
        //$check_access = $this->m_db->where(array('time_vs' => $time_vs, 'mac' => $mac))->count('log_access_' . $year);
        $check_access = 0;
        $checkSession = 1;
        //die($check_access == "" ? "Empty" : $check_access);
        //die($checkSession == "" ? "Empty" : $checkSession);
        if ($check_access == 0 && $nasid != null && $code == 0 && $checkSession == 1) {
            //if($NameDeviceVer == 'Other' && $OsNameVer != 'Windows'){}
            //else 

            if ($visit == 2 && $res == 'notyet' && $OsNameVer != null && $BrowerName != null && $BrowerNameVer != null) {
                $arr_log = array(
                    'browser_name' => $BrowerNameVer,
                    'browser_name_group' => $BrowerName,
                    'os_name' => $OsNameVer,
                    'device_name' => $NameDeviceVer,
                    'ip_access' => $Ip,
                    'os_group' => $OsName,
                    'id_shop' => $id_place,
                    'hours_access' => intval(date('H')),
                    'minutes_access' => intval(date('i')),
                    'seconds_access' => intval(date('s')),
                    'day_access' => intval(date('d')),
                    'month_access' => intval(date('m')),
                    'year_access' => intval(date('Y')),
                    'id_campaign' => intval($id_campaign),
                    'time_access' => date('Y-m-d H:i:s'),
                    'show_time' => date('d-m-Y'),
                    'show_full_time' => date('H:i:s d-m-Y'),
                    'time_vs' => $time_vs,
                    'mac' => $mac,
                    'called' => $nasid,
                    'cfvd' => $Ip
                );
                $this->m_db->insert('log_access_' . $year, $arr_log);

                // VOID() - lưu last_online
                $check = $this->m_db->where(array('mac' => $mac))->count('last_online');
                if ($check == 0) {
                    $arr_last = array(
                        'mac' => $mac,
                        'time' => date('Y-m-d H:i:s'),
                        'time_vs' => $time_vs,
                        'id_place' => $id_place
                    );
                    $this->m_db->insert('last_online', $arr_last);
                } else {
                    $arr_last = array(
                        'time' => date('Y-m-d H:i:s'),
                        'time_vs' => $time_vs,
                        'id_place' => $id_place
                    );
                    $this->m_db->set($arr_last)->where(array('mac' => $mac))->update('last_online', array(), array());
                }
                //
                return '200';
            } else {
                return '202';
            }
        } else {
            return '201';
        }
        /*
          if($called == 'E8-DE-27-39-81-6D')
          {
          $data = (object)array('folder_campaign' => 'html2', 'link_redirect' => 'http://www.icook.com.vn', 'id_campaign' => 2);
          }else if($called == 'E8-DE-27-39-81-6C')
          {
          $data = (object)array('folder_campaign' => 'html1', 'link_redirect' => 'http://www.icook.com.vn', 'id_campaign' => 46);
          } */
    }

    public function sendAuthCode() {
        $code = $this->generateCode(4);
        $mobile = $this->getParamString('phonenumner');
        $_mobi = substr($mobile, 1);
        $content = urlencode("Chao ban! \nMa xac thuc cua ban la: " . $code);
        $mac = $_GET['mac'] != null ? $_GET['mac'] : $this->session->userdata('mac');
        $nasid = $_GET['nasid'] != null ? $_GET['nasid'] : $this->session->userdata('nasid');
        $datetime = date('Y-m-d H:i:s');

        $arr = array(
            'mac' => $mac,
            'nasid' => $nasid,
            'mobile' => $mobile,
            'otp' => $code,
            'amount_sms' => 1,
            'created_sms' => $datetime,
            'created' => $datetime
        );
        if ($this->db->query('SELECT auth_device.id, DATE_FORMAT(auth_device.created_sms,"%Y-%m-%d") AS times, auth_device.amount_sms FROM auth_device WHERE auth_device.mac = "' . $mac . '" AND auth_device.mobile = "' . $mobile . '"')->num_rows() == 0) {
            $sendSMS = json_decode(file_get_contents('http://118.70.109.212:1234/?c=' . $content . '&m=' . $_mobi . '&t_=1423453843'));

            if ($sendSMS->status == 200) {
                $this->db->insert('auth_device', $arr);
                $_SESSION['auth'] = 200;
                return 200;
            } else {
                return 203;
            }
        } else {
            $date = date('Y-m-d');
            $qr = $this->db->query('SELECT auth_device.id, DATE_FORMAT(auth_device.created_sms,"%Y-%m-%d") AS times, auth_device.amount_sms FROM auth_device WHERE DATE_FORMAT(auth_device.created_sms,"%Y-%m-%d") = "' . $date . '" AND auth_device.mac = "' . $mac . '" AND auth_device.mobile = "' . $mobile . '"');
            $checkSMSAmount = $qr->num_rows();
            if ($checkSMSAmount > 0) {
                $dataSMSAmount = $qr->row_object();
                if ($dataSMSAmount->amount_sms < 10) {
                    $sendSMS = json_decode(file_get_contents('http://118.70.109.212:1234/?c=' . $content . '&m=' . $_mobi . '&t_=1423453843'));
                    if ($sendSMS->status == 200) {
                        $this->db->update('auth_device', array('otp' => $code, 'amount_sms' => $dataSMSAmount->amount_sms + 1, 'created_sms' => $datetime), "id = " . $dataSMSAmount->id);
                        return 200;
                    } else {
                        return 203;
                    }
                }
            } else {
                $dataSMSAmount = $qr->row_object();
                $sendSMS = json_decode(file_get_contents('http://118.70.109.212:1234/?c=' . $content . '&m=' . $_mobi . '&t_=1423453843'));
                if ($sendSMS->status == 200) {
                    $this->db->update('auth_device', array('otp' => $code, 'amount_sms' => '1', 'created_sms' => $datetime), "id = " . $dataSMSAmount->id);
                    return 200;
                } else {
                    return 203;
                }
            }
        }
        return 201;
    }

    public function checkAuthCode() {
        $auth_code = $this->getParamString('auth_code');
        $mac = str_replace('.html', '', $this->getParamString('mac') ? $this->getParamString('mac') : $this->session->userdata('mac'));

        $verify = $this->db->query("SELECT id,otp FROM auth_device WHERE mac = '" . $mac . "' AND otp = '" . $auth_code . "'")->row_object();
        if ($auth_code == $verify->otp) {
            $this->db->update('auth_device', array('active' => 1), "mac = '" . $mac . "' AND id = " . $verify->id);
            return 200;
        }
        return 201;
    }

    public function checkAuth() {
        $mac = $_GET['mac'] ? $_GET['mac'] : $this->session->userdata('mac');
        $count = $this->db->query("SELECT id FROM auth_device WHERE mac = '" . $mac . "' AND active = 1")->num_rows();
        return $count;
    }

    public function saveAccessTokenDB($user_fb, $token, $mac, $name, $email) {
        $count = $this->db->query("SELECT id FROM token_key WHERE macs = '" . $mac . "'")->num_rows();
        if ($count == 0) {
            $this->db->insert('token_key', array('idfb' => $user_fb, 'token' => $token, 'macs' => $mac, 'names' => $name, 'emails' => $email));
        }
    }

    public function getAccessTokenDB($mac) {
        $sql = $this->db->query("SELECT * FROM `token_key` WHERE `macs` = '" . $mac . "'");
        $count = $sql->num_rows();

        if ($count > 0) {
            $profile = (object) array();
            $data = $sql->row_object();
            $this->session->set_userdata(array('token' => $data->token, 'fbid' => $data->idfb));
            $profile->id = $data->idfb;
            $profile->name = $data->names;
            $profile->email = $data->emails;

            return array('status' => 200, 'urlLogin' => null, 'user_profile' => $profile, 'ctmf' => 2);
        } else {
            return array('status' => 201);
        }
    }

    public function getListJob($id) {
        $data = $this->db->query('SELECT * FROM vn_job ORDER BY ordering ASC')->result_object();
        $html = '<select name="job" id="job"><option value = "">Chọn nghề nghiệp</option>';
        foreach ($data AS $el => $le) {
            $el1 = $el + 1;
            if ($el1 == $id) {
                $selected = 'selected="selected"';
            } else {
                $selected = '';
            }
            $html .= '<option value="' . $el1 . '" ' . $selected . '>' . $le->name . '</option>';
        } $html .='</select>';
        return $html;
    }

    // 1.chiến dịch like fanpage
    public function insertlikefbDB($mac, $nasid, $id_place, $facebook_url, $id_campaign, $fbid, $email, $fullname, $name_device) {
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'fbid' => $fbid,
            'email' => $email,
            'fullname' => $fullname,
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'facebook_url' => $facebook_url
        );
//        echo json_encode($arr);
//        die();
        $this->m_db->insert('log_facebook', $arr);
        return '200';
    }

    // 2.chiến dịch share url fb
    /* public function campaignsharelinkfacebook($mac, $nasid) {
      $facebook_url = $this->getParamString('facebook_url');
      $id_campaign = $this->getParamString('id_campaign');
      $fbid = $this->getParamString('fbid');
      $email = $this->getParamString('email');
      $fullname = $this->getParamString('fullname');

      $name_device = $this->getParamString('name_device');
      $id_place = $this->db->query('SELECT id_place FROM modem WHERE nasid = "' . $nasid . '"')->row_object()->id_place;

      ini_set('mongo.native_long', 1);
      $now = date("Y-m-d H:i:s", time());
      $nr = date('YmdHis');
      $cd__ = new MongoInt64($nr);
      $arr = array(
      'fbid' => $fbid,
      'email' => $email,
      'fullname' => $fullname,
      'id_place' => intval($id_place),
      'time_access' => date('Y-m-d H:i:s'),
      'show_time' => date('d-m-Y'),
      'show_full_time' => date('H:i:s d-m-Y'),
      'time_vs' => $cd__,
      'mac' => $mac,
      'nasid' => $nasid,
      'id_campaign' => $id_campaign,
      'name_device' => $name_device,
      'facebook_url' => $facebook_url
      );
      $this->m_db->insert('log_facebook', $arr);
      return '200';
      } */

    // 3.truy cập website
    public function campaignaccesswebsite($mac, $nasid) {
        $website_url = $this->getParamString('website_url');
        $id_campaign = $this->getParamString('id_campaign');

        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;

        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'website_url' => $website_url
        );
        $this->m_db->insert('log_accessweb', $arr);
        return '200';
    }

    // 4.xem ảnh banner quảng cáo 
    public function campaignviewbannerads($mac, $nasid) {
        $image_url = $this->getParamString('image_url');
        $id_campaign = $this->getParamString('id_campaign');

        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nasid = "' . $nasid . '"')->row_object()->id_place;

        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'image_url' => $image_url
        );
        $this->m_db->insert('log_viewbanner', $arr);
        return '200';
    }

    // 5. xem video quảng cáo
    public function campaignviewvideoads($mac, $nasid) {
        $video_url = $this->getParamString('video_url');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nasid = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'video_url' => $video_url
        );
        $this->m_db->insert('log_viewvideo', $arr);
        return '200';
    }

    // 6 cài app
    public function campaigninstallapp($mac, $nasid) {
        $name_app = $this->getParamString('name_app');
        $download_url = $this->getParamString('download_url');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nasid = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_app' => $name_app,
            'name_device' => $name_device,
            'download_url' => $download_url
        );
        $this->m_db->insert('log_setupapp', $arr);
        return '200';
    }

    //7 thu thập ý kiến khách hàng log_survey
    public function campaigncustomerreview($mac, $nasid) {
        $data = $this->getParamString('data');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nasid = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'data' => $data
        );
        $this->m_db->insert('log_survey', $arr); //sau
        return '200';
    }

    // 8 thu thập email 
    /* public function campaignemail($mac, $nasid) {
      $email = $this->getParamString('email');
      $id_campaign = $this->getParamString('id_campaign');
      $name_device = $this->getParamString('name_device');
      $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
      ini_set('mongo.native_long', 1);
      $now = date("Y-m-d H:i:s", time());
      $nr = date('YmdHis');
      $cd__ = new MongoInt64($nr);
      $arr = array(
      'id_place' => intval($id_place),
      'time_access' => date('Y-m-d H:i:s'),
      'show_time' => date('d-m-Y'),
      'show_full_time' => date('H:i:s d-m-Y'),
      'time_vs' => $cd__,
      'mac' => $mac,
      'nasid' => $nasid,
      'id_campaign' => $id_campaign,
      'name_device' => $name_device,
      'email' => $email
      );
      $this->m_db->insert('log_email', $arr);
      return '200';
      } */

    // 9 số đt 
    public function campaignmobile($mac, $nasid) {
        $mobile = $this->getParamString('mobile');
        $id_campaign = $this->getParamString('id_campaign');
        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'mobile' => $mobile
        );
        $this->m_db->insert('log_mobile', $arr);
        return '200';
    }

    public function checkFacebooklike($mac, $facebook_url, $id_campaign) {
        $query = $this->m_db->where(array('mac' => $mac, 'facebook_url' => $facebook_url, 'id_campaign' => (int) $id_campaign))->limit(1);
        $result = $query->get('log_facebook');
        if (count($result) == 0) {
            return '200';
        }
        return json_encode($result);
    }

    public function checkEmail($mac, $id_campaign) {
        $query = $this->m_db->where(array('mac' => trim($mac), 'id_campaign' => (int) $id_campaign))->limit(1);
        $result = $query->get('log_email');

        if (count($result) == 0) {
            return '200';
        }
        return json_encode($result);
    }

    public function checkPhone($mac, $id_campaign) {
        $query = $this->m_db->where(array('mac' => trim($mac), 'id_campaign' => (int) $id_campaign))->limit(1);
        $result = $query->get('log_mobile');

        if (count($result) == 0) {
            return '200';
        }
        return json_encode($result);
    }

    public function checkLoginWifi($mac, $id_campaign) {
        $result = $this->m_db->where(array('mac' => trim($mac), 'id_campaign' => (int) $id_campaign))
                ->order_by(array('show_full_time' => 'DESC'))
                ->limit(1)
                ->get('log_wifipassword');
        if (count($result) == 0) {
            return '200';
        }
        return json_encode($result);
    }

    public function checkCoupon($mac, $id_campaign, $type_coupon) {
        if ($type_coupon == 1) { // 1 lần
            $query = $this->m_db->where(array('mac' => trim($mac), 'id_campaign' => (int) $id_campaign))->limit(1);
            $result = $query->get('log_campaign_coupon');
            if (count($result) == 0) {
                return '200'; // chưa nhận => hiển thị bình thường để khách hàng nhận 
            } else {
                return json_encode($result); // đã nhận trả về thông tin => hiển thị truy cập miễn phí
            }
        } else { // nhiều lần
            $query = $this->m_db->where(array('mac' => trim($mac), 'id_campaign' => (int) $id_campaign, 'active' => 1))->limit(1);
            $result = $query->get('log_coupon_mobile');
            if (count($result) == 0) {
                return '200'; // ko có coupon nào nhận mà chưa dùng=> hiển thị bình thường để nhận tiếp 
            }
            return json_encode($result); // đã nhận, chưa sử dụng=>  trả về thông tin => hiển thị truy cập miễn phí
        }
    }

}
